define(['../../common/service','drop'],function( http){
    var peopleManage = {
        init: function () {
            var _this = this;
            //列表管理
            $('table[list-manager="user-list"]').listManager('init', {
                supportAjaxPage: false,		//是否支持配置列表ajxa分页[多列表两种状态都有时，统一配置为true,禁用的列表pageJson参数为空即可]
                supportAdjust	: false		//是否支持宽度调整
                ,supportDrag	: false 		//是否支持拖拽换位
                ,supportConfig	: false 		//是否支持配置列表功能[操作列是否可见]
                ,supportSetTop	: false		//是否支持表头置顶
            }, function(){
                _this._initList({});
            });

            _this.bindEvent();

            _this.initRegistrationCodeModal();
            _this.removeModal();
        },
        bindEvent : function(){
            var _this = this;
            //查询
            _this.search();
            //注销
            _this.cancel();
            //新员工注册码
            _this.registrationCode();
        },
        _initList : function(_par){
            var _this = this;
            var _container = $(".people-manage-container");
            _container.find("tbody").empty();
            Kit.loading.show($(_container));
            http.work('/user/findByUsernameOrEmail',_par,'POST').then(function(res){
                Kit.loading.hide($(_container));
                if(res.flag == 'success'){
                    _this.renderTable(res.data)
                }else{
                    Kit.alert2(res.msg || I18N.getText('peopleManage','failed'));
                }
            });
        },
        registrationCode : function(){
            var _container = $(".people-manage-container");
            _container.find(".add").off("click").on("click", function(){
                $('#registrationCode-modal').modal({
                    backdrop: 'static',
                    keyboard: false
                });
                $('#registrationCode-modal').modal("show");
            });
        },
        removeModal : function(){
            var _removeModal = $('#remove-modal');
            _removeModal.find(".btn-primary").off("click").on("click", function(e){
                var loadingAray = $('#remove-modal .modal-dialog');
                Kit.loading.show(loadingAray);
                $('tr[id="' + 2 + '"]').remove();
                var _par = {
                    userId :  _removeModal.find("input").val()
                }
                http.work('/user/delete',_par,'POST').then(function(res){
                    Kit.loading.hide($(loadingAray));
                    if(res.flag == 'success'){
                        //删除用户
                        $('tr[id="' + _par.userId + '"]').remove();
                        $.each($('td[name="order-td"]'), function(i, _orderTd){
                            $(_orderTd).html(i + 1);
                        });
                        if ($("tbody>tr").length == 0){
                            var _html = '<tr><td colspan="5" style="font-size: 34px;color: #777;height: 211px;">'
                                + I18N.getText('peopleManage','noUser')+'</td></tr>';
                            $("tbody").html(_html);
                        }
                    }else{
                        Kit.alert2(res.msg || I18N.getText('peopleManage','failed'));
                    }
                    _removeModal.modal("hide");
                },function(){
                    Kit.loading.hide($(loadingAray));
                    _removeModal.modal("hide");
                });
            });
        },
        initRegistrationCodeModal : function(){
            var _registrationCodeModal = $('#registrationCode-modal');
            _registrationCodeModal.find(".role-list").select2({
                minimumResultsForSearch: -1,
                data:  [
                    { id: 1, text: I18N.getText('peopleManage','admin') },
                    { id: 2, text: I18N.getText('peopleManage','advertiser')},
                    { id: 3, text: I18N.getText('peopleManage','designer')}
                ]
            });
            _registrationCodeModal.find(".num-list").select2({
                minimumResultsForSearch: -1,
                data:  [
                    { id: 1, text: 1},
                    { id: 2, text: 2},
                    { id: 3, text: 3},
                    { id: 4, text: 4},
                    { id: 5, text: 5},
                    { id: 6, text: 6},
                    { id: 7, text: 7},
                    { id: 8, text: 8},
                    { id: 9, text: 9},
                    { id: 10, text: 10}
                ]
            });
            //生成注册码
            _registrationCodeModal.find(".generate").off("click").on("click", function(){
                var _codeArea = $(".code-area");
                _codeArea.empty();
                var loadingAray = $('#registrationCode-modal .modal-dialog');
                Kit.loading.show(loadingAray);
                var _par = {
                    roleId: $(".role-list").val(),
                    num: $(".num-list").val()
                };

                http.work('/user/generateRegisterCode',_par,'POST').then(function(res){
                    Kit.loading.hide($(loadingAray));
                    if(res.flag == 'success'){
                        var results = '';
                        $.each(res.data, function(i, _code){
                            if (i > 0 && i != 5){
                                results += ' ';
                            }
                            results += '<span>' + _code.code + '</span>';
                        });
                        _codeArea.html(results);
                    }else{
                        Kit.alert2(res.msg || I18N.getText('peopleManage','failed'));
                    }
                },function(){
                    Kit.loading.hide($(loadingAray));
                });
            });
            //modal打开时
            _registrationCodeModal.on('show.bs.modal', function () {
                _registrationCodeModal.find(".role-list").val(1).trigger("change");
                _registrationCodeModal.find(".num-list").val(1).trigger("change");
                var _codeArea = $(".code-area");
                _codeArea.empty();
            });
        },
        search : function(){
            var _this = this;
            var _container = $(".people-manage-container");
            _container.find(".search").off("click").on("click", function(){
                var par = {
                    usernameOrEmailStr : _container.find(".search-input").val()
                }
                _this._initList(par);
            });
        },
        cancel : function(){
            var _container = $(".people-manage-container");
            _container.off("click", ".cancel-b").on("click", ".cancel-b", function(e){
                var _tr = $(e.target).parents("tr");
                var _user = $(_tr).data("userInfo");
                $("#remove-modal").find(".modal-body .remind").html(I18N.getText('peopleManage','removeConfirm', _user.username,  _user.email));
                $("#remove-modal").find(".modal-body input").val(_user.id);
                $('#remove-modal').modal({
                    backdrop: 'static',
                    keyboard: false
                });
                $("#remove-modal").modal("show");
            });
        },

        renderTable : function(_datas){
            var _container = $(".people-manage-container");
            var _html = '';
            if(!_datas || _datas.length == 0){
                _html += '<tr><td colspan="5" style="font-size: 34px;color: #777;height: 211px;">'
                    + I18N.getText('peopleManage','noUser')+'</td></tr>';
                _container.find("tbody").html(_html);
            } else {
                $.each(_datas, function(i, _data){
                    _html = '<tr id="' + _data.id + '">'
                        + '<td name="order-td">' + (i + 1) + '</td>'
                        + '<td>' + _data.email + '</td>'
                        + '<td>' + _data.username + '</td>';
                    //role
                    var _roleShows = '';
                    var _roleShow;
                    $.each(_data.roleId, function(j, _role){
                        if (j > 0){
                            _roleShows += ',';
                        }

                        if (1 == _role){
                            //管理员
                            _roleShow = I18N.getText('peopleManage','admin');
                        } else if (2 == _role){
                            //投放师
                            _roleShow = I18N.getText('peopleManage','advertiser');
                        } else if (3 == _role){
                            //设计师
                            _roleShow = I18N.getText('peopleManage','designer');
                        } else if (4 == _role){
                            //设计师
                            _roleShow = I18N.getText('peopleManage','advertiser');
                        } else {
                            _roleShow = '';
                        }
                        _roleShows += _roleShow;
                    });
                    _html +=  '<td>' + _roleShows + '</td>';
                    _html +=  '<td><a class="cancel-b icon-btn"><i class="glyphicon glyphicon-trash"></i></a></td></tr>';
                    _container.find("tbody").append($(_html).data("userInfo", _data));
                });
            }
        }
    }

    return peopleManage;
});
